//
//  MainViewController.m
//  DongChiDaChiProject1
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "MainViewController.h"
#import "MJRefresh.h"
#import "DataHandle.h"
#import "HotMeiShiCollectionViewCell.h"
#import "SDCycleScrollView.h"
#import "MeiZhouViewController.h"
#import "QianTangViewController.h"
#import "TeSeXiaoChiViewController.h"
#import "LiangCaiViewController.h"
#import "ShiShangYinPinViewController.h"
#import "TianPinViewController.h"
#import "ShiLiaoYaoShanViewController.h"
#import "HongPeiViewController.h"
#import "DetailUIViewController.h"
#import "AllKindViewController.h"
#import <AFNetworking.h>
#import "AppDelegate.h"
#import "UserManager.h"


@interface MainViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SDCycleScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

// 在第一个section上添加scrollView
@property (nonatomic, strong) UIScrollView *backgroudScrollView;

// 热门推荐更多按钮
@property (nonatomic, strong) UIButton *hotButton;

// 建立一个数组保存8张图片
@property (nonatomic, strong) NSMutableArray *eightPictureArray;

// 首页上的轮播图
@property (nonatomic, strong) NSMutableArray *scrollPictureArray;

// 建立一个图片数组存储轮播图的图片
@property (nonatomic, strong) NSMutableArray *scrollPicture;
// 建立一个标题数组存储轮播图的标题
@property (nonatomic, strong) NSMutableArray *scrollTitle;
// 记录下拉的次数
@property (nonatomic, assign) int pullCount;

// 记录是否是程序刚开始
@property (nonatomic, assign) int isGangKaiShi;

// 判断是否请求数据
@property (nonatomic, assign) BOOL isEndRequestData;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
    self.navigationItem.title = @"菜谱";
    // 检测网络
   [self searchNet];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //[self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];

   
    
    
    // 设置bar
    [self setBar];
//    // 添加collectionView
    [self addView];
    
    self.backgroudScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 210 * kWidthFitParameter)];
    
    // 建立一个数组保存8张图片
    self.eightPictureArray = [NSMutableArray arrayWithObjects:@"http://pub.szzhangchu.com/web/images/nav/nav8.png",@"http://pub.szzhangchu.com/web/images/nav/nav13.png", @"http://pub.szzhangchu.com/web/images/nav/nav5.png", @"http://pub.szzhangchu.com/web/images/nav/nav14.png", @"http://pub.szzhangchu.com/web/images/nav/nav11.png", @"http://pub.szzhangchu.com/web/images/nav/nav16.png", @"http://pub.szzhangchu.com/web/images/nav/nav7.png", @"http://pub.szzhangchu.com/web/images/nav/nav4.png", nil];
    
    // 给pullcount赋初值
    _pullCount = 0;
    _isGangKaiShi = 0;
    _isEndRequestData = NO;
    
    
}


//- (BOOL)shouldAutorotate   // 是否允许旋转
//{
//        return NO;
//}
//
//
//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
//
//{
//    
//    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
//    
//}

#pragma mark - 检测网络
- (void)searchNet
{
    // 程序刚开始需要默认为有网络
    [[UserManager shareUserManagerHandle] saveNet:@"YES"];
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    // 在网络变化的情况下自动会调用此回调方法
    // 开启检测网络状态
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       // AFNetworkReachabilityStatusUnknown          = -1, // 不可知
        //        AFNetworkReachabilityStatusNotReachable     = 0, // 无网络
        //        AFNetworkReachabilityStatusReachableViaWWAN = 1, // 2d或者3d
        //        AFNetworkReachabilityStatusReachableViaWiFi = 2, // wifi
         NSLog(@"status +++++++++++++++++++++++++++++++++ %ld", status);
        if (status == 0) {
            // 说明没有网络
            [[UserManager shareUserManagerHandle] saveNet:@"NO"];
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"该软件只有在网络状态下才能使用" preferredStyle:UIAlertControllerStyleAlert];
           
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                UIWindow *win = [UIApplication sharedApplication].windows.firstObject;
//                [win.rootViewController presentViewController:alertVC animated:YES completion:nil];
                
                // 退出程序
//                AppDelegate *app = [UIApplication sharedApplication].delegate;
//                UIWindow *window = app.window;
//                
//                [UIView animateWithDuration:1.0f animations:^{
//                    window.alpha = 0;
//                    window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
//                } completion:^(BOOL finished) {
//                    exit(0);
//                }];
                
                
            }];
             [alertVC addAction:alertAction];
            [self presentViewController:alertVC animated:YES completion:nil];
            
        } else {
            [[UserManager shareUserManagerHandle] saveNet:@"YES"];
            // 上啦刷新
            [self pullFresh];

        }
       
    }];
    
    
}

#pragma mark - 界面将要消失


- (void)viewWillDisappear:(BOOL)animated
{
    // 发送隐藏tabbar的通知
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
}



// 界面已经呈现

- (void)viewDidAppear:(BOOL)animated
{
    
//   [[NSNotificationCenter defaultCenter] postNotificationName:@"SHOWTABBAR" object:nil];
}

// 添加collectionView
- (void)addView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:0.1];
    [self.view addSubview:_collectionView];
    // 注册
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"123"];
    [self.collectionView registerClass:[HotMeiShiCollectionViewCell class] forCellWithReuseIdentifier:@"HotMeiShiCollectionViewCell"];
    
    // 表头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header2"];
    
   
    
}

// 上拉刷新
- (void)pullFresh
{
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(xialaFresh)];
    
    self.collectionView.footer = [MJRefreshBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(shanglashuaxin)];
    
    [self.collectionView.header beginRefreshing];
}

#pragma mark - 上啦刷新
- (void)shanglashuaxin
{
   
    NSLog(@"count = %d", _pullCount);
    switch (_pullCount) {
        case 1:
        {
            if (_isEndRequestData == YES) {
                [[DataHandle shareHandle] requestHotMeiShiDataWithPage:2 AndReturn:^(NSMutableArray *array) {
                    self.scrollPictureArray = [NSMutableArray arrayWithArray:array];
                    _pullCount = 2;
                    _isEndRequestData = YES;
                    [self.collectionView reloadData];
                }];
                // block的执行速度小于这句话的执行速度
                _isEndRequestData = NO;
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"您不要狂拉本页面哦！吃吃正在为您努力加载数据" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alert show];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [alert dismissWithClickedButtonIndex:0 animated:YES];
                });

            }
            
            break;
        }
            case 2:
        {
            if (_isEndRequestData == YES) {
                [[DataHandle shareHandle] requestHotMeiShiDataWithPage:3 AndReturn:^(NSMutableArray *array) {
                    self.scrollPictureArray = [NSMutableArray arrayWithArray:array];
                    _pullCount = 3;
                    _isEndRequestData = YES;
                    [self.collectionView reloadData];
                }];
                _isEndRequestData = NO;
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"您不要狂拉本页面哦！吃吃正在为您努力加载数据" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alert show];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [alert dismissWithClickedButtonIndex:0 animated:YES];
                });
            }
            break;
        }
         case 3:
        {
            if (_isEndRequestData == YES) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"目前还没有新的哦！" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [alertView show];
                // alertView消失
                [self performSelector:@selector(dismissAlertView:) withObject:alertView afterDelay:1.0];
            }
            
            break;
        }
       
        default:
             break;
            
    }
    [self.collectionView.footer endRefreshing];
}

- (void)dismissAlertView:(UIAlertView *)alertView
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}

#pragma mark - 下啦刷新的方法
- (void)xialaFresh
{
 
    if (_isGangKaiShi == 0) {
        [[DataHandle shareHandle] deleteShouYeSaveArray];
        // 建立数据源
        [[DataHandle shareHandle] requestHotMeiShiDataWithPage:1 AndReturn:^(NSMutableArray *array) {
            // NSLog(@"array = %@", array);
            
            self.scrollPictureArray = [NSMutableArray arrayWithArray:array];
            
            _pullCount = 1;
            _isEndRequestData = YES;
            [self.collectionView reloadData];
            
            
        }];
        _isGangKaiShi = 1;
    }
    
    
    [self.collectionView.header endRefreshing];
}

#pragma mark - setbar
- (void)setBar
{
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithHue:0.1 saturation:0.3 brightness:1 alpha:1]];
    
}

#pragma mark - collectionView的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return [[DataHandle shareHandle] returnCountOfHotMeiShiModel];
}

#pragma mark - 设置表头
// 设置表头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        UICollectionReusableView *headerViewSection1 = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1" forIndexPath:indexPath];
        headerViewSection1.backgroundColor = [UIColor whiteColor];
        // 解析轮播图的数据
        NSLog(@"self.scrollPictureArray--------------------------------------------------------- = %ld", self.scrollPictureArray.count);
        
        if (self.scrollPictureArray.count > 0) {
            self.scrollPicture = [NSMutableArray array];
            self.scrollTitle = [NSMutableArray array];
            for (HotMeiShiModel *model in self.scrollPictureArray) {
                // 赋值
                [self.scrollPicture addObject:[model valueForKey:@"thumb_2"]];
                [self.scrollTitle addObject:[model valueForKey:@"title"]];
            }
//            NSLog(@"self.scrollPicture = %@", self.scrollPicture);
//            NSLog(@"self.scrollTitle = %@", self.scrollTitle);
            NSMutableArray *tempScrollPicture = [NSMutableArray array];
            NSMutableArray *tempScrollTitle = [NSMutableArray array];
            NSLog(@"_pullCount = %d", _pullCount);
                switch (_pullCount) {
                    case 1:
                    {
                        if (self.scrollPicture.count > 0 && self.scrollTitle.count > 0) {
                            for (int i = 0; i < 5; i++) {
                                [tempScrollPicture addObject:self.scrollPicture[i]];
                                [tempScrollTitle addObject:self.scrollTitle[i]];
                            }
                        }
                       
                        break;
                    }
                    case 2:
                    {
                        if (self.scrollPicture.count > 12 && self.scrollTitle.count > 12) {
                            for (int i = 12; i < 12 + 5; i++) {
                                [tempScrollPicture addObject:self.scrollPicture[i]];
                                [tempScrollTitle addObject:self.scrollTitle[i]];
                            }
                        }
                        
                        break;
                    }
                    case 3:
                    {
                        if (self.scrollPicture.count > 18 && self.scrollTitle.count > 18) {
                            for (int i = 18; i < 18 + 5; i++) {
                                [tempScrollPicture addObject:self.scrollPicture[i]];
                                [tempScrollTitle addObject:self.scrollTitle[i]];
                            }
                        }
                        
                        break;
                    }
                    default:
                    {
                        if (self.scrollPicture.count >= 18 && self.scrollTitle.count >= 18) {
                            for (int i = 18; i < 18 + 5; i++) {
                                [tempScrollPicture addObject:self.scrollPicture[i]];
                                [tempScrollTitle addObject:self.scrollTitle[i]];
                            }
                        }
                       
                        break;
                    }
                }

            
            
            
            // 建立轮播图
            SDCycleScrollView *scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 200) imageURLStringsGroup:tempScrollPicture];
            scrollView.titlesGroup = tempScrollTitle;
            scrollView.delegate = self;
            scrollView.autoScrollTimeInterval = 2;
            [headerViewSection1 addSubview:scrollView];
            
            
        }
        
        
       
        
        
        return headerViewSection1;
    } else {
        UICollectionReusableView *headerViewSection2 = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header2" forIndexPath:indexPath];
        headerViewSection2.backgroundColor = [UIColor whiteColor];
        UILabel *hotLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 80, 40)];
        
        hotLabel.text = @"热门推荐";
        hotLabel.textColor = [UIColor orangeColor];
        [headerViewSection2 addSubview:hotLabel];
        
//        self.hotButton = [UIButton buttonWithType:UIButtonTypeSystem];
//        self.hotButton.frame = CGRectMake(kScreenWidth - 50, 10, 40, 40);
//        [self.hotButton setTitle:@"更多" forState:UIControlStateNormal];
//        [self.hotButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//        [self.hotButton addTarget:self action:@selector(actionToHotMore:) forControlEvents:UIControlEventTouchUpInside];
//        [headerViewSection2 addSubview:_hotButton];
        
        return headerViewSection2;
    }
    
    
    
}

#pragma mark - 轮播图的点击方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
      NSLog(@"点击选中的第%ld张.", index + 1);
    DetailUIViewController *detailVC = [[DetailUIViewController alloc] init];
    //detailVC.meiZhouModel = (MeiZhouModel *)[[DataHandle shareHandle] returnHotMeiShiModelByIndex:indexPath];
   
    if (self.scrollPictureArray.count > index) {
        switch (_pullCount) {
            case 1:
            {
                // 取出第一组的array
                detailVC.meiZhouModel = self.scrollPictureArray[index];
                break;
            }
            case 2:
            {
                // 取出第二组的array
                detailVC.meiZhouModel = self.scrollPictureArray[index + 12];
                
                break;

            }
            case 3:
            {
                // 取出第三组的array
                detailVC.meiZhouModel = self.scrollPictureArray[index + 18];
                break;

            }
            default:
                break;
        }
    }
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:detailVC];
    [self presentViewController:naVC animated:YES completion:nil];
}

#pragma mark - 热门推荐的更多button
- (void)actionToHotMore:(UIButton *)button
{
    NSLog(@"更多");
}

// 返回表头的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(kScreenWidth, 200);
    }
    return CGSizeMake(kScreenWidth, 60);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    if (indexPath.section == 0 && indexPath.row == 0) {
         UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"123" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor whiteColor];
        
        self.backgroudScrollView.contentSize = CGSizeMake(kScreenWidth, 220 * kWidthFitParameter);
        self.backgroudScrollView.scrollEnabled = NO;
        self.backgroudScrollView.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:_backgroudScrollView];
        
        // 添加八个button和label
        // 靓汤image
        UIImageView *qiantangImage = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kWidthFitParameter, 20 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter)];
        qiantangImage.layer.cornerRadius = 50 * kWidthFitParameter / 2;
        qiantangImage.backgroundColor = [UIColor lightGrayColor];
        
        qiantangImage.userInteractionEnabled = YES;
        [qiantangImage sd_setImageWithURL:[NSURL URLWithString:self.eightPictureArray[0]] placeholderImage:nil];
        qiantangImage.tag = 200;
        [self.backgroudScrollView addSubview:qiantangImage];
        
        // 靓汤label
        UILabel *qiantangLabel = [[UILabel alloc] initWithFrame:CGRectMake(qiantangImage.left, qiantangImage.bottom + 5 * kWidthFitParameter, 50 * kWidthFitParameter, 20 * kWidthFitParameter)];
       // qiantangLabel.backgroundColor = [UIColor cyanColor];
        qiantangLabel.text = @"靓汤";
        qiantangLabel.font = [UIFont systemFontOfSize:12 * kWidthFitParameter];
        qiantangLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroudScrollView addSubview:qiantangLabel];
        
        // 为imageView添加手势
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToGap:)];
        [qiantangImage addGestureRecognizer:tapGes];
        
        // 特色小吃image
        UIImageView *featureImage = [[UIImageView alloc] initWithFrame:CGRectMake(qiantangImage.right + 45 * kWidthFitParameter, 20 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter)];
        featureImage.layer.cornerRadius = 50 * kWidthFitParameter / 2;
        featureImage.backgroundColor = [UIColor lightGrayColor];
        [featureImage sd_setImageWithURL:[NSURL URLWithString:self.eightPictureArray[1]] placeholderImage:nil];
        featureImage.userInteractionEnabled = YES;
        featureImage.tag = 201;
        [self.backgroudScrollView addSubview:featureImage];
        
        // 特色小吃label
        UILabel *featureLabel = [[UILabel alloc] initWithFrame:CGRectMake(featureImage.left, featureImage.bottom + 5 * kWidthFitParameter, 50 * kWidthFitParameter, 20 * kWidthFitParameter)];
        //featureLabel.backgroundColor = [UIColor cyanColor];
        featureLabel.text = @"特色小吃";
        featureLabel.font = [UIFont systemFontOfSize:12 * kWidthFitParameter];
        featureLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroudScrollView addSubview:featureLabel];
        
      
        
        // 为特色小吃添加手势
        UITapGestureRecognizer *featureTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToGap:)];
        [featureImage addGestureRecognizer:featureTapGes];
        
        // 凉菜image
        UIImageView *liangCaiImage = [[UIImageView alloc] initWithFrame:CGRectMake(featureImage.right + 45 * kWidthFitParameter, 20 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter)];
        liangCaiImage.layer.cornerRadius = 50 * kWidthFitParameter / 2;
        liangCaiImage.backgroundColor = [UIColor lightGrayColor];
         [liangCaiImage sd_setImageWithURL:[NSURL URLWithString:self.eightPictureArray[2]] placeholderImage:nil];
        liangCaiImage.userInteractionEnabled = YES;
        liangCaiImage.tag = 202;
        [self.backgroudScrollView addSubview:liangCaiImage];
        
        // 凉菜label
        UILabel *liangCaiLabel = [[UILabel alloc] initWithFrame:CGRectMake(liangCaiImage.left, liangCaiImage.bottom + 5 * kWidthFitParameter, 50 * kWidthFitParameter, 20 * kWidthFitParameter)];
       // liangCaiLabel.backgroundColor = [UIColor cyanColor];
        liangCaiLabel.text = @"凉菜";
        liangCaiLabel.font = [UIFont systemFontOfSize:12 * kWidthFitParameter];
        liangCaiLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroudScrollView addSubview:liangCaiLabel];
        
        
        
        // 为凉菜添加手势
        UITapGestureRecognizer *liangCaiTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToGap:)];
        [liangCaiImage addGestureRecognizer:liangCaiTapGes];
        
        // 时尚饮品image
        
        UIImageView *fashionImage = [[UIImageView alloc] initWithFrame:CGRectMake(liangCaiImage.right + 45 * kWidthFitParameter, 20 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter)];
        fashionImage.layer.cornerRadius = 50 * kWidthFitParameter / 2;
        fashionImage.backgroundColor = [UIColor lightGrayColor];
         [fashionImage sd_setImageWithURL:[NSURL URLWithString:self.eightPictureArray[3]] placeholderImage:nil];
        fashionImage.userInteractionEnabled = YES;
        fashionImage.tag = 203;
        [self.backgroudScrollView addSubview:fashionImage];
        
        // 时尚饮品label
        UILabel *fashionLabel = [[UILabel alloc] initWithFrame:CGRectMake(fashionImage.left, fashionImage.bottom + 5 * kWidthFitParameter, 50 * kWidthFitParameter, 20 * kWidthFitParameter)];
      //  fashionLabel.backgroundColor = [UIColor cyanColor];
        fashionLabel.text = @"时尚饮品";
        fashionLabel.font = [UIFont systemFontOfSize:12 * kWidthFitParameter];
        fashionLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroudScrollView addSubview:fashionLabel];
        
        
        
        // 为时尚饮品添加手势
        UITapGestureRecognizer *fashionTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToGap:)];
        [fashionImage addGestureRecognizer:fashionTapGes];
        
        // 甜品image
        
        UIImageView *sugarImage = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kWidthFitParameter, qiantangLabel.bottom + 20 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter)];
        sugarImage.layer.cornerRadius = 50 * kWidthFitParameter / 2;
        sugarImage.backgroundColor = [UIColor lightGrayColor];
         [sugarImage sd_setImageWithURL:[NSURL URLWithString:self.eightPictureArray[4]] placeholderImage:nil];
        sugarImage.userInteractionEnabled = YES;
        sugarImage.tag = 204;
        [self.backgroudScrollView addSubview:sugarImage];
        
        // 甜品label
        UILabel *sugarLabel = [[UILabel alloc] initWithFrame:CGRectMake(sugarImage.left, sugarImage.bottom + 5 * kWidthFitParameter, 50 * kWidthFitParameter, 20 * kWidthFitParameter)];
      //  sugarLabel.backgroundColor = [UIColor cyanColor];
        sugarLabel.text = @"甜品";
        sugarLabel.font = [UIFont systemFontOfSize:12 * kWidthFitParameter];
        sugarLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroudScrollView addSubview:sugarLabel];
        
        
        
        // 为甜品添加手势
        UITapGestureRecognizer *sugarTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToGap:)];
        [sugarImage addGestureRecognizer:sugarTapGes];

        
        // 食疗药膳image
        
        UIImageView *shiLiaoImage = [[UIImageView alloc] initWithFrame:CGRectMake(sugarImage.right + 45 * kWidthFitParameter, qiantangLabel.bottom + 20 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter)];
        shiLiaoImage.layer.cornerRadius = 50 * kWidthFitParameter / 2;
        shiLiaoImage.backgroundColor = [UIColor lightGrayColor];
         [shiLiaoImage sd_setImageWithURL:[NSURL URLWithString:self.eightPictureArray[5]] placeholderImage:nil];
        shiLiaoImage.userInteractionEnabled = YES;
        shiLiaoImage.tag = 205;
        [self.backgroudScrollView addSubview:shiLiaoImage];
        
        // 食疗药膳label
        UILabel *shiLiaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(shiLiaoImage.left, shiLiaoImage.bottom + 5 * kWidthFitParameter, 50 * kWidthFitParameter, 20 * kWidthFitParameter)];
      //  shiLiaoLabel.backgroundColor = [UIColor cyanColor];
        shiLiaoLabel.text = @"食疗药膳";
        shiLiaoLabel.font = [UIFont systemFontOfSize:12 * kWidthFitParameter];
        shiLiaoLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroudScrollView addSubview:shiLiaoLabel];
        
        
        
        // 为食疗药膳添加手势
        UITapGestureRecognizer *shiliaoTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToGap:)];
        [shiLiaoImage addGestureRecognizer:shiliaoTapGes];
        
        
        // 烘培image
        
        UIImageView *barbecueImage = [[UIImageView alloc] initWithFrame:CGRectMake(shiLiaoImage.right + 45 * kWidthFitParameter, qiantangLabel.bottom + 20 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter)];
        barbecueImage.layer.cornerRadius = 50 * kWidthFitParameter / 2;
        barbecueImage.backgroundColor = [UIColor lightGrayColor];
         [barbecueImage sd_setImageWithURL:[NSURL URLWithString:self.eightPictureArray[6]] placeholderImage:nil];
        barbecueImage.userInteractionEnabled = YES;
        barbecueImage.tag = 206;
        [self.backgroudScrollView addSubview:barbecueImage];
        
        // 烘培label
        UILabel *barbecueLabel = [[UILabel alloc] initWithFrame:CGRectMake(barbecueImage.left, barbecueImage.bottom + 5 * kWidthFitParameter, 50 * kWidthFitParameter, 20 * kWidthFitParameter)];
      //  barbecueLabel.backgroundColor = [UIColor cyanColor];
        barbecueLabel.text = @"美粥 ";
        barbecueLabel.font = [UIFont systemFontOfSize:12 * kWidthFitParameter];
        barbecueLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroudScrollView addSubview:barbecueLabel];
        
        
        
        // 为烘培添加手势
        UITapGestureRecognizer *barbucueTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToGap:)];
        [barbecueImage addGestureRecognizer:barbucueTapGes];
        
        // 全部分类image
        
        UIImageView *allImage = [[UIImageView alloc] initWithFrame:CGRectMake(barbecueImage.right + 45 * kWidthFitParameter, qiantangLabel.bottom + 20 * kWidthFitParameter, 50 * kWidthFitParameter, 50 * kWidthFitParameter)];
        allImage.layer.cornerRadius = 50 * kWidthFitParameter / 2;
        allImage.backgroundColor = [UIColor lightGrayColor];
        [allImage sd_setImageWithURL:[NSURL URLWithString:self.eightPictureArray[7]] placeholderImage:nil];
        allImage.userInteractionEnabled = YES;
        allImage.tag = 207;
        [self.backgroudScrollView addSubview:allImage];
        
        // 全部分类label
        UILabel *allLabel = [[UILabel alloc] initWithFrame:CGRectMake(allImage.left, allImage.bottom + 5 * kWidthFitParameter, 50 * kWidthFitParameter, 20 * kWidthFitParameter)];
       // allLabel.backgroundColor = [UIColor cyanColor];
        allLabel.text = @"全部分类";
        allLabel.font = [UIFont systemFontOfSize:12 * kWidthFitParameter];
        allLabel.textAlignment = NSTextAlignmentCenter;
        [self.backgroudScrollView addSubview:allLabel];
        
        
        
        // 为全部分类添加手势
        UITapGestureRecognizer *allTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionToGap:)];
        [allImage addGestureRecognizer:allTapGes];

        
        return cell;
        
    } else {
        HotMeiShiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotMeiShiCollectionViewCell" forIndexPath:indexPath];

        cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.model = [[DataHandle shareHandle] returnHotMeiShiModelByIndex:indexPath];
        return cell;
    }
    
    
}

#pragma mark - 点击imageView的手势
- (void)actionToGap:(UITapGestureRecognizer *)tapGesture
{
    switch (tapGesture.view.tag) {
        case 200:
        {
            NSLog(@"靓汤");
            QianTangViewController *qiantangVC = [[QianTangViewController alloc] init];
          //  [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
            [self.navigationController pushViewController:qiantangVC animated:YES];
            break;
        }
            case 201:
        {
            NSLog(@"特色小吃");
            TeSeXiaoChiViewController *teSeXiaoChiVC = [[TeSeXiaoChiViewController alloc] init];
            //[[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
            [self.navigationController pushViewController:teSeXiaoChiVC animated:YES];
            break;
        }
            case 202:
        {
            NSLog(@"凉菜");
            LiangCaiViewController *liangCaiVC = [[LiangCaiViewController alloc] init];
            //[[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
            [self.navigationController pushViewController:liangCaiVC animated:YES];
            break;
        }
            case 203:
        {
            
            NSLog(@"时尚饮品");
            ShiShangYinPinViewController *shiShangYinPinVC = [[ShiShangYinPinViewController alloc] init];
          //  [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
            [self.navigationController pushViewController:shiShangYinPinVC animated:YES];
            break;
        }
            case 204:
        {
            NSLog(@"甜品");
            TianPinViewController *tianPinVC = [[TianPinViewController alloc] init];
            //[[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
            [self.navigationController pushViewController:tianPinVC animated:YES];
            break;
        }
            case 205:
        {
            NSLog(@"食疗药膳");
            ShiLiaoYaoShanViewController *shiLiaoYaoShanVC = [[ShiLiaoYaoShanViewController alloc] init];
           // [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
            [self.navigationController pushViewController:shiLiaoYaoShanVC animated:YES];
            break;
        }
            case 206:
        {
            NSLog(@"美粥");
            MeiZhouViewController *meiZhouVC = [[MeiZhouViewController alloc] init];
           // [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
            [self.navigationController pushViewController:meiZhouVC animated:YES];
            break;
        }
            case 207:
        {
            NSLog(@"全部分类");
            AllKindViewController *allKindVC = [[AllKindViewController alloc] init];
           // [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDDENTABBAR" object:nil];
            
            [self.navigationController pushViewController:allKindVC  animated:YES];
            
            break;
        }
        default:
            break;
    }
}




// 每个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(kScreenWidth, 210 * kWidthFitParameter);
    }
    return CGSizeMake(kScreenWidth, 230 * kWidthFitParameter);
}

// 每个分区的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return UIEdgeInsetsMake(10 * kWidthFitParameter, 0, 10 * kWidthFitParameter, 0);
    } else {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    
}

#pragma mark - 点击时间
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"热门美食indexPath.section = %ld, row = %ld", (long)indexPath.section, indexPath.row);
    DetailUIViewController *detailVC = [[DetailUIViewController alloc] init];
    detailVC.meiZhouModel = (MeiZhouModel *)[[DataHandle shareHandle] returnHotMeiShiModelByIndex:indexPath];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:detailVC];
    [self presentViewController:naVC animated:YES completion:nil];
}

// 每个item之间的jiange
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 50;
//}
//
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
